package com.gopay.common.rcs.service.rims;

import com.gopay.common.constants.cps.ChildTranType;
import com.gopay.common.constants.trans.CurrencyCode;
import com.gopay.common.constants.trans.PayChannel;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.domain.cust.CustCorpInfo;
import com.gopay.common.domain.cust.CustPersonInfo;
import com.gopay.common.domain.rims.RimsEvaluateParas;
import com.gopay.common.domain.rims.RimsEvaluateParas.ParaKey;
import com.gopay.common.user.manager.CustCorpInfoQueryManager;
import com.gopay.common.user.manager.CustPersonInfoQueryManager;
import com.gopay.common.util.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class RimsTransServiceImpl implements RimsTransService {
	protected final static Log logger = LogFactory.getLog(RimsTransServiceImpl.class);
	@Autowired
	protected CustCorpInfoQueryManager custCorpInfoService;
	@Resource(name = "custPersonInfoQueryManager")
	protected CustPersonInfoQueryManager custPersonInfoQueryManager;

	@Override
	public RimsEvaluateParas initParasFromOrder(CpsGenMainOrder order) {
		RimsEvaluateParas paras = new RimsEvaluateParas();
		if (IntTxnCd._01302.value.equals(order.getGopayIntTxnCd())) {
			if (ChildTranType._GP_1.value.equals(order.getChildTranType()) || ChildTranType._GP_4.value.equals(order.getChildTranType())) {
				return paras;
			}
		}
		paras.putPara(ParaKey.CUST_NO, order.getPayCustId());//默认设置付款方，如果需要配置其他用户，在下面的分交易代码逻辑中设置
		paras.putPara(ParaKey.TRANS_ORDER_ID, order.getGopayOrderId());
		paras.putPara(ParaKey.TRANS_TIME, DateUtils.format(order.getGopayTxnTm(), "yyyy-MM-dd HH:mm:ss"));
		paras.putPara(ParaKey.PAY_ACCOUNT, order.getPayAcct());
		paras.putPara(ParaKey.PAY_CUST_ID, order.getPayCustId());
		paras.putPara(ParaKey.PAY_AMOUNT, order.getMerTxnAmt().toString());
		paras.putPara(ParaKey.PAY_CUST_TYPE, PayChannel.get(order.getPayChannel()).isCorpType() ? "2" : "1");
		paras.putPara(ParaKey.REC_ACCOUNT, order.getMainRecvAcct());
		paras.putPara(ParaKey.REC_CUST_ID, order.getMainRecvCustId());
		paras.putPara(ParaKey.CURRENCY_TYPE, CurrencyCode.CNY.name().equals(order.getCurType()) ? CurrencyCode.CNY.value : order.getCurType());
		if (IntTxnCd._00100.value.equals(order.getGopayIntTxnCd())) {
		    paras.putPara(ParaKey.CUST_NO, order.getMainRecvCustId());
			if (StringUtils.isNotBlank(order.getPayCustId())) {//未登录使用银行卡支付时，没有PayCustId.
				paras.putPara(ParaKey.PAY_CUST_NAME, getCustName(order.getGopayOrderId(), order.getPayChannel(), order.getPayCustId()));
			}
			//新平台商户只能是企业，这里固定为PayChannel._51按照企业来查用户。
			paras.putPara(ParaKey.REC_CUST_NAME, getCustName(order.getGopayOrderId(), PayChannel._51.toString(), order.getMainRecvCustId()));
			paras.putPara(ParaKey.REC_CUST_TYPE, "2");//00100交易收款方都是企业
			return paras;
		} else if (IntTxnCd._00200.value.equals(order.getGopayIntTxnCd())) {
			if (StringUtils.isNotBlank(order.getPayCustId())) {//未登录使用银行卡支付时，没有PayCustId.
				paras.putPara(ParaKey.PAY_CUST_NAME, getCustName(order.getGopayOrderId(), order.getPayChannel(), order.getPayCustId()));
			}
			//大宗商户只能是企业，这里固定为PayChannel._51按照企业来查用户。
			paras.putPara(ParaKey.REC_CUST_NAME, getCustName(order.getGopayOrderId(), PayChannel._51.toString(), order.getMainRecvCustId()));
			paras.putPara(ParaKey.REC_CUST_TYPE, "2");//大宗商户只能是企业,00400交易收款方都是企业
			return paras;
		} else if (IntTxnCd._00400.value.equals(order.getGopayIntTxnCd())) {
		    paras.putPara(ParaKey.CUST_NO, order.getMainRecvCustId());
			if (StringUtils.isNotBlank(order.getPayCustId())) {//未登录使用银行卡支付时，没有PayCustId.
				paras.putPara(ParaKey.PAY_CUST_NAME, getCustName(order.getGopayOrderId(), order.getPayChannel(), order.getPayCustId()));
			}
			//新平台商户只能是企业，这里固定为PayChannel._51按照企业来查用户。
			paras.putPara(ParaKey.REC_CUST_NAME, getCustName(order.getGopayOrderId(), PayChannel._51.toString(), order.getMainRecvCustId()));
			paras.putPara(ParaKey.REC_CUST_TYPE, "2");//新平台商户只能是企业,00400交易收款方都是企业
			return paras;
		} else if (IntTxnCd._00900.value.equals(order.getGopayIntTxnCd())) {
		    paras.putPara(ParaKey.PAY_CUST_NAME, getCustName(order.getGopayOrderId(), order.getPayChannel(), order.getPayCustId()));
		    paras.putPara(ParaKey.REC_CUST_NAME, getCustName(order.getGopayOrderId(), order.getPayChannel(), order.getMainRecvCustId()));
		    paras.putPara(ParaKey.REC_CUST_TYPE, getCustType(order.getMainRecvCustId()));
		    return paras;
		} else if (IntTxnCd._00700.value.equals(order.getGopayIntTxnCd())) {
		    paras.putPara(ParaKey.PAY_CUST_NAME, getCustName(order.getGopayOrderId(), order.getPayChannel(), order.getPayCustId()));
			return paras;
		} else if (IntTxnCd._00701.value.equals(order.getGopayIntTxnCd())) { //退款转账 add by zhangyu
			paras.putPara(ParaKey.PAY_CUST_NAME, getCustName(order.getGopayOrderId(), order.getPayChannel(), order.getPayCustId()));
			return paras;
		} else if (IntTxnCd._00500.value.equals(order.getGopayIntTxnCd())) {
			paras.putPara(ParaKey.REC_CUST_NAME, getCustName(order.getGopayOrderId(), order.getPayChannel(), order.getMainRecvCustId()));
			paras.putPara(ParaKey.ACCT_PRODUCT, order.getMainRecvAcctProdCode());
			paras.putPara(ParaKey.REC_CUST_TYPE, getCustType(order.getMainRecvCustId()));
			return paras;
		} else if (IntTxnCd._00207.value.equals(order.getGopayIntTxnCd())) {
			paras.putPara(ParaKey.PAY_CUST_NAME, getCustName(order.getGopayOrderId(), order.getPayChannel(), order.getPayCustId()));
			//大宗市场入金 商户只能是企业，这里固定为PayChannel._51按照企业来查用户。
			paras.putPara(ParaKey.REC_CUST_NAME, getCustName(order.getGopayOrderId(), PayChannel._51.toString(), order.getMainRecvCustId()));
			paras.putPara(ParaKey.REC_CUST_TYPE, "2");
			return paras;
		} else if (IntTxnCd._00201.value.equals(order.getGopayIntTxnCd())) {
			paras.putPara(ParaKey.PAY_CUST_NAME, getCustName(order.getGopayOrderId(), order.getPayChannel(), order.getPayCustId()));
			//大宗划拨收益 商户只能是企业，这里固定为PayChannel._51按照企业来查用户。
			paras.putPara(ParaKey.REC_CUST_NAME, getCustName(order.getGopayOrderId(), PayChannel._51.toString(), order.getMainRecvCustId()));
			paras.putPara(ParaKey.REC_CUST_TYPE, "2");
			return paras;
		} else if (IntTxnCd._00202.value.equals(order.getGopayIntTxnCd())) {
			paras.putPara(ParaKey.PAY_CUST_NAME, getCustName(order.getGopayOrderId(), order.getPayChannel(), order.getPayCustId()));
			//大宗出金商户只能是企业，这里固定为PayChannel._51按照企业来查用户。
			paras.putPara(ParaKey.REC_CUST_NAME, getCustName(order.getGopayOrderId(), PayChannel._51.toString(), order.getMainRecvCustId()));
			paras.putPara(ParaKey.REC_CUST_TYPE, "2");
			return paras;

		} else if (IntTxnCd._01300.value.equals(order.getGopayIntTxnCd())) {
			paras.putPara(ParaKey.REC_CUST_NAME, getCustName(order.getGopayOrderId(), order.getPayChannel(), order.getMainRecvCustId()));
			paras.putPara(ParaKey.REC_CUST_TYPE, PayChannel.get(order.getPayChannel()).isCorpType() ? "2" : "1");
			return paras;
		} else if (IntTxnCd._01303.value.equals(order.getGopayIntTxnCd())) {
			return paras;
		} else if (IntTxnCd._02301.value.equals(order.getGopayIntTxnCd())) {
			return paras;
		}else {

			logger.warn("此交易类型[" + order.getGopayIntTxnCd() + "]不做RIMS风控数据初始化. orderId=" + order.getGopayOrderId());
			return null;
		}

	}
	
	private String getCustType(String custId) {
	    if(StringUtils.isNotBlank(custId)) {
	        CustCorpInfo corpInfo = custCorpInfoService.get(custId);
	        if(corpInfo != null) {
	            return "2";
	        }
	        else {
	            return "1";
	        }
	    }
	    return null;
	}

	private String getCustName(String orderId, String payChannel, String custId) {
	    //按照建议用户类型查找
		if (PayChannel.get(payChannel).isCorpType()) {
			CustCorpInfo corpInfo = null;
			corpInfo = custCorpInfoService.get(custId);
			if (null != corpInfo) {
				return corpInfo.getCorpName();
			} else {
				logger.error("订单类型为企业，但是找不到CustCorpInfo! orderId=" + orderId + ", payChannel=" + payChannel + ", custId=" + custId);
			}
		} else {
			CustPersonInfo personInfo = null;
			personInfo = custPersonInfoQueryManager.get(custId);
			if (null != personInfo) {
				return personInfo.getCustName();
			} else {
				logger.error("订单类型为个人，但是找不到CustPersonInfo! orderId=" + orderId + ", payChannel=" + payChannel + ", custId=" + custId);
			}
		}
		
		//按照非建议用户类型查找，如果上边逻辑没有查找成功，说明建议的PAYCHANNEL类型错误
		if (PayChannel.get(payChannel).isCorpType()) {
		    CustPersonInfo personInfo = null;
            personInfo = custPersonInfoQueryManager.get(custId);
            if (null != personInfo) {
                return personInfo.getCustName();
            } else {
                logger.error("订单类型为个人，但是找不到CustPersonInfo! orderId=" + orderId + ", payChannel=" + payChannel + ", custId=" + custId);
            }
        } else {
            CustCorpInfo corpInfo = null;
            corpInfo = custCorpInfoService.get(custId);
            if (null != corpInfo) {
                return corpInfo.getCorpName();
            } else {
                logger.error("订单类型为企业，但是找不到CustCorpInfo! orderId=" + orderId + ", payChannel=" + payChannel + ", custId=" + custId);
            }
        }
		return null;
	}
}
